﻿using Alexinea.Autofac.Extensions.DependencyInjection;
using Autofac;
using Microsoft.Extensions.DependencyInjection;
using Sample.Core;
using Sample.Core.Models;
using Sample.Core.Services;
using System;

namespace Sample.Topshelf.Extensions
{
    public static class ServicesExtensions
    {
        public static ContainerBuilder ConfigureSelf(this ContainerBuilder builder)
        {
            var services = new ServiceCollection();
            // register appsettings.json
            services.Configure<User>("UserConfig", Settings.Instance.Configuration.GetSection("User"));
            builder.Populate(services);
            // register services repositories
            builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly)
                   .Where(t => t.Name.EndsWith("Repository"))
                   .AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(typeof(UserService).Assembly)
                 .Where(t => t.Name.EndsWith("Service"))
                 .AsImplementedInterfaces();

            return builder;
        }
    }
}
